package cn.iocoder.yudao.module.eval.controller.admin.filingoccupation;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.eval.controller.admin.filingoccupation.vo.*;
import cn.iocoder.yudao.module.eval.dal.dataobject.filingoccupation.FilingOccupationDO;
import cn.iocoder.yudao.module.eval.service.filingoccupation.FilingOccupationService;

@Tag(name = "管理后台 - 申请开展评价的职业（工种）关联")
@RestController
@RequestMapping("/eval/filing-occupation")
@Validated
public class FilingOccupationController {

    @Resource
    private FilingOccupationService filingOccupationService;

    @PostMapping("/create")
    @Operation(summary = "创建申请开展评价的职业（工种）关联")
    @PreAuthorize("@ss.hasPermission('eval:filing-occupation:create')")
    public CommonResult<Long> createFilingOccupation(@Valid @RequestBody FilingOccupationSaveReqVO createReqVO) {
        return success(filingOccupationService.createFilingOccupation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新申请开展评价的职业（工种）关联")
    @PreAuthorize("@ss.hasPermission('eval:filing-occupation:update')")
    public CommonResult<Boolean> updateFilingOccupation(@Valid @RequestBody FilingOccupationSaveReqVO updateReqVO) {
        filingOccupationService.updateFilingOccupation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除申请开展评价的职业（工种）关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('eval:filing-occupation:delete')")
    public CommonResult<Boolean> deleteFilingOccupation(@RequestParam("id") Long id) {
        filingOccupationService.deleteFilingOccupation(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除申请开展评价的职业（工种）关联")
                @PreAuthorize("@ss.hasPermission('eval:filing-occupation:delete')")
    public CommonResult<Boolean> deleteFilingOccupationList(@RequestParam("ids") List<Long> ids) {
        filingOccupationService.deleteFilingOccupationListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得申请开展评价的职业（工种）关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('eval:filing-occupation:query')")
    public CommonResult<FilingOccupationRespVO> getFilingOccupation(@RequestParam("id") Long id) {
        FilingOccupationDO filingOccupation = filingOccupationService.getFilingOccupation(id);
        return success(BeanUtils.toBean(filingOccupation, FilingOccupationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得申请开展评价的职业（工种）关联分页")
    @PreAuthorize("@ss.hasPermission('eval:filing-occupation:query')")
    public CommonResult<PageResult<FilingOccupationRespVO>> getFilingOccupationPage(@Valid FilingOccupationPageReqVO pageReqVO) {
        PageResult<FilingOccupationDO> pageResult = filingOccupationService.getFilingOccupationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FilingOccupationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出申请开展评价的职业（工种）关联 Excel")
    @PreAuthorize("@ss.hasPermission('eval:filing-occupation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFilingOccupationExcel(@Valid FilingOccupationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FilingOccupationDO> list = filingOccupationService.getFilingOccupationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "申请开展评价的职业（工种）关联.xls", "数据", FilingOccupationRespVO.class,
                        BeanUtils.toBean(list, FilingOccupationRespVO.class));
    }

}